<!--
 * @Description: 菜单
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2024-03-07
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2025-03-11
-->
<script lang="ts" setup>
  import { computed } from 'vue'
  import { useRoute, useRouter } from 'vue-router'

  import { MenuImgEnum } from '/@/enums/img/layoutEnum'
  import { RouterPathEnum } from '/@/enums/routerEnum'
  import { useMenu } from '/@/store/modules/menu'

  const route = useRoute()
  const router = useRouter()

  const menu = useMenu()

  /*
  watch(
    () => menu.activeNo,
    () => {
      const firstMenu = menu.subMenus[0]
      const secondMenu = firstMenu.children[0]
      openMenu(firstMenu.permissionUrl + secondMenu.permissionUrl)
    },
  )
  */

  /**
   * 打开菜单
   * @param url 菜单链接
   */
  const openMenu = (url: string) => router.push(url)

  /** 菜单选中 */
  const menuActive = computed(() => (url: string) => route.path.indexOf(url) === 0)

  /**
   * 校验菜单是否展开
   */
  const checkMenuExpanded = (menu: GetPermissionByUserIdTypes.IMenu) => !(menu.expanded === false)

  /**
   * 展开菜单
   * menu 菜单
   */
  const expandMenu = (menu: GetPermissionByUserIdTypes.IMenu) => (menu.expanded = !checkMenuExpanded(menu))
</script>

<template>
  <div class="menu">
    <div class="menu-group" key="menu-group-home">
      <div class="menu-group-title home" :class="menuActive(RouterPathEnum.HOME) ? 'active' : ''" @click="openMenu(RouterPathEnum.HOME)">
        <img class="logo" :src="MenuImgEnum.HOME_ACTIVE" />
        <span class="title">工作台</span>
      </div>
    </div>
    <div class="menu-group" v-for="(item, i) in menu.subMenus" :key="`menu-group${i}`">
      <div v-if="item.children.length" class="menu-group-title" :class="menuActive(item.url) ? 'active' : ''">
        <img class="logo" :src="MenuImgEnum.OPERATION_ACTIVE" />
        <span class="title">{{ item.name }}</span>
        <img class="arrow" :src="MenuImgEnum.ARROW_BOTTOM" @click="expandMenu(item)" />
      </div>
      <div v-else class="menu-only-tab" :class="menuActive(item.url) ? 'active' : ''" @click="openMenu(item.url)">
        <img class="logo" :src="MenuImgEnum.OPERATION_ACTIVE" />
        <span class="title">{{ item.name }}</span>
      </div>
      <div class="menu-group-child" :class="menuActive(item.url + subItem.url) ? 'active' : ''" v-for="(subItem, subI) in item.children" :key="`menu-group-item${subI}`" v-show="checkMenuExpanded(item)" @click="openMenu(item.url + subItem.url)"> {{ subItem.name }} </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
  .menu {
    padding: 16px 0;
  }

  .menu-group {
    .menu-group-title {
      height: 40px;
      padding: 0 17px;
      display: flex;
      align-items: center;

      .logo {
        width: 14px;
        height: 14px;
      }

      .title {
        width: 150px;
        padding-left: 11px;
        font-size: 14px;
        color: rgb(0 0 0 / 65%);
      }

      .arrow {
        width: 12px;
        height: 12px;
        cursor: pointer;
      }

      &.active {
        .title {
          font-weight: bold;
          color: @primary-color;
        }
      }

      &.home {
        cursor: pointer;
      }
    }

    .menu-only-tab {
      height: 40px;
      padding: 0 17px;
      display: flex;
      align-items: center;
      cursor: pointer;

      .logo {
        width: 14px;
        height: 14px;
      }

      .title {
        width: 150px;
        padding-left: 11px;
        font-size: 14px;
        color: rgb(0 0 0 / 65%);
      }

      &.active {
        color: @primary-color;
        background: #e8fffb;
        border-right: 2px solid @primary-color;
      }
    }

    .menu-group-child {
      height: 40px;
      padding-left: 42px;
      line-height: 40px;
      color: rgb(0 0 0 / 65%);
      cursor: pointer;

      &.active {
        color: @primary-color;
        background: #e8fffb;
        border-right: 2px solid @primary-color;
      }
    }
  }
</style>
